package com.frxuexi.mymvi

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.hilt.navigation.compose.hiltViewModel
import coil.compose.AsyncImage
import com.frxuexi.mymvi.entity.RecordData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@SuppressLint("CoroutineCreationDuringComposition")
@Composable
fun NewsPage(scope:CoroutineScope= rememberCoroutineScope(),
             vm:MessViewModel= hiltViewModel()) {

    var list = remember {
        SnapshotStateList<RecordData>()
    }

    scope.launch {
        vm.state.collect{
            when(it){
                is MessState.onSuccess->{
                    Log.i("===",it.list.toString())
                    list.addAll(it.list)
                }
                is MessState.onError->{
                    Log.i("===",it.msg)
                }
                else->{

                }
            }
        }
    }

    scope.launch {
        vm.channel.send(MessIntent.getMess(1))
    }

    LazyColumn(content = {
        items(list){
            Column {
                Text(text = it.title)
                AsyncImage(model = it.imgurl, contentDescription = "")
            }
        }
    })

}